package _17_剑指Offer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class _61_剑指Offer扑克牌中的顺子 {

    public static void main(String[] args) {

        _61_剑指Offer扑克牌中的顺子 v = new _61_剑指Offer扑克牌中的顺子();

        int[] ins = new int[]{0, 0, 1, 2, 5};
        System.out.println(v.isStraight(ins));
    }

    // 最大差值法
    public boolean isStraight(int[] nums) {
        Set<Integer> set = new HashSet<>();
        int min = 14;
        int max = 0;
        for (int num : nums) {
            if (num == 0) continue;
            if (set.contains(num)) return false;
            min = Math.min(min, num);
            max = Math.max(max, num);
            set.add(num);
        }
        return max - min < 5;
    }

    // 排序统计法则
    public boolean isStraight1(int[] nums) {
        // 排序
        Arrays.sort(nums);
        // 计算0的个数
        int zeroCount = 0;
        int lastIndex = nums.length - 1;
        int diff = 0;
        for (int i = 0; i < lastIndex; i++) {
            if (nums[i] == 0) {
                zeroCount++;
            } else {
                if (nums[i] == nums[i + 1]) return false;
                if (nums[i] + 1 != nums[i + 1]) {
                    diff += nums[i + 1] - nums[i] - 1;
                }
            }
        }
        return zeroCount >= diff;
    }

}
